package org.jeecg.jt808.processor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.jt808.JTT808Message;
import org.jeecg.jt808.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.jt808.modbus.ModbusParams;
import org.jeecg.jt808.util.JsonUtil;
import org.jeecg.jt808.util.UniqueSerialNumberGenerator;

public class JT808MessageProcessor {


    private final JT808MessageGeneratorFactory messageGeneratorFactory=new JT808MessageGeneratorFactory();
    private final ObjectMapper objectMapper = new ObjectMapper(); //  ObjectMapper 作为成员变量






    public String generateJT808Message(String sim, MessageType messageType, Jt808CentralControlConfig config, ModbusParams modbusParams, Integer customSeq) {

        Integer seq = (customSeq != null) ? customSeq : Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence()); // 允许自定义流水号
        String hexSeq = String.format("%04x", seq);


        try {
            JTT808Message message = new JTT808Message();
            message.setSim(sim);
            message.setSequence(seq);

            JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType.name());

            String encodedMessage;
            if (modbusParams != null) {
                encodedMessage = generator.generateMessage(message, modbusParams);
            } else if (config != null) {
                encodedMessage = generator.generateMessage(message, config);
            } else {
                encodedMessage = generator.generateMessage(message);
            }

            // 返回编码后的数据，而不是 JSON 字符串
            return encodedMessage;


        } catch (Exception e) {
            // 处理异常 - 记录日志或抛出异常
            //  建议添加日志记录
            throw new RuntimeException("生成JT808消息失败", e);
        }
    }


    public String process(String sn,String imei, MessageType messageType, Jt808CentralControlConfig config) {
        Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
        String sendJson=null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String sim = imei;


            // 生成编码信息
            JTT808Message message = new JTT808Message();
            message.setSim(sim);
            message.setSequence(seq);
            JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType);
            String encodedMessage = generator.generateMessage(message,config);
            sendJson = JsonUtil.createMqttMessageJson(encodedMessage, sn,imei, messageType.getType(), String.valueOf(seq));


        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sendJson;
    }


    public String process(String sn,String imei,MessageType messageType) {
        Integer seq = Integer.parseInt(UniqueSerialNumberGenerator.getNextSequence());
        String sendJson=null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String sim = imei;


            // 生成编码信息
            JTT808Message message = new JTT808Message();
            message.setSim(sim);
            message.setSequence(seq);
            JT808MessageGenerator generator = messageGeneratorFactory.getGenerator(messageType);
            String encodedMessage = generator.generateMessage(message);


            sendJson = JsonUtil.createMqttMessageJson(encodedMessage, sn,imei, messageType.getType(), String.valueOf(seq));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sendJson;
    }


}
